class TrajectoryManager:
    """
    统一管理轨迹与拖尾
    """
    def __init__(self, max_trace_points=100, max_trail_points=30):
        """
        :param max_trace_points 最多保存多少个轨迹点
        :param max_trail_points 最多保存多少个拖尾点
        """
        self.max_trace = max_trace_points
        self.max_trail = max_trail_points
        # 储存轨迹数据
        # 字典 name 为键， 值为轨迹点的坐标列表
        self.traces    = {}  # name -> {'x': [], 'y': []}

    def add_body(self, name):
        self.traces[name] = {'x': [], 'y': []}

    def update_position(self, name, x, y):
        trace = self.traces[name]
        trace['x'].append(x)
        trace['y'].append(y)
        # 限制长度
        if len(trace['x']) > self.max_trace:
            trace['x'].pop(0)
            trace['y'].pop(0)

    def get_trace_data(self, name):
        t = self.traces[name]
        return t['x'], t['y']

    def get_trail_data(self, name):
        # 拖尾 = 最近 max_trail 个点
        t = self.traces[name]
        return t['x'][-self.max_trail:], t['y'][-self.max_trail:]